<template>
    <div id="login">
        <div id="loginBox">
            <div class="g_pic" align="center">
                <img src="../assets/专利.png"/>
            </div>
            <el-form ref="UserFormRef" :model="UserForm" :rules="UserRules" class="user_form" label-width="0px">
                <el-form-item prop="name" class="inps">
                    <el-input v-model="UserForm.username" prefix-icon="iconfont icon-yonghu"></el-input>
                    <span id="nameInfo"></span>
                </el-form-item>
                <el-form-item prop="password" class="inps">
                    <el-input type="password" v-model.number="UserForm.password"
                              prefix-icon="iconfont icon-mima"></el-input>
                    <span id="numberInfo"></span>
                </el-form-item>
                <el-form-item prop="tel" class="inps">
                    <el-input v-model.number="UserForm.tel"
                              prefix-icon="iconfont icon-dianhua"></el-input>
                    <span id="telInfo"></span>
                </el-form-item>

                <el-form-item class="btns" align="center">
                    <el-button type="primary" class="submitBtn" @click="addUser()">注册</el-button>
                    <el-button type="info" class="submitBtn" @click="resetForm()">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                UserForm: {
                    userID:"",
                    username: "",
                    password: "",
                    tel:""//telephone
                },
                UserRules: {
                    username: [{
                        required: true,
                        message: '用户名为必填项',
                        trigger: 'blur'
                    },
                        {
                            min: 2,
                            max: 8,
                            message: '长度在 2 到 8 个字符',
                            trigger: 'blur'
                        }
                    ],
                    password: [{
                        required: true,
                        message: '密码为必填项',
                        trigger: 'blur'
                    },],
                }
            };
        },
        methods: {
            resetForm() {
                this.$refs.UserFormRef.resetFields()
            },
            addUser() {
                this.$refs.UserFormRef.validate(async valid => {
                    console.log(this.UserForm);
                    if (!valid) return;
                    const {data:resString} = await this.$http.post("addUser",this.UserForm);//②接收后台返回的string数据：含有两块内容--flag和student <--| = |--> ①访问后台
                    var res=JSON.parse(JSON.stringify(resString));//转换为json对象
                    console.log(res);
                    if(res=="ok"){
                        window.sessionStorage.setItem('username', this.UserForm.username);
                        window.sessionStorage.setItem('password', this.UserForm.password);
                        this.$message.success("注册成功");
                        await this.$router.push({path: "/home"});
                    }else{
                        this.$message.error("注册失败");
                    }
                })
            }
        }
    };
</script>

<style lang='less' scoped>
    #login {
        width: 100vw;
        padding: 0;
        margin: 0;
        height: 100vh;
        font-size: 16px;
        background-repeat: no-repeat;
        background-position: left top;
        background-color: #242645;
        color: #fff;
        font-family: "Source Sans Pro";
        background-size: 100%;
        background-image: url(../assets/背景.png);
        position: relative;

        #bgd {
            height: 100vh;
            width: 100vw;
            overflow: hidden;
        }

        #loginBox {
            width: 400px;
            height: 320px;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            padding: 50px 40px 40px 40px;
            box-shadow: -15px 15px 15px rgba(6, 17, 47, 0.7);
            opacity: 1;
            background: linear-gradient(230deg,
            rgba(53, 57, 74, 0) 0%,
            rgb(0, 0, 0) 100%);

            /deep/ .inps input {
                border: none;
                color: #fff;
                background-color: transparent;
                font-size: 18px;
            }

            .submitBtn {
                background-color: transparent;
                color: #39f;
                width: 100px;
            }

            .iconfont {
                color: #fff;
            }
        }
    }

    .user_form {
        padding: 0 20px;
    }
</style>
